package controller;

import model.Order;
import model.User;
import service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "OrderConfirm", value = "/order_confirm")
public class OrderConfirmServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        Order o = (Order) session.getAttribute("order");
        User user = (User) session.getAttribute("user");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        int paytype = Integer.parseInt(req.getParameter("paytype"));
        o.setAddress(address);
        o.setName(name);
        o.setPaytype(paytype);
        o.setPhone(phone);
        o.setUser(user);
        //调用service
        OrderService o1 = new OrderService();
        boolean flag = o1.addOrder(o);
        if (flag) {
            session.removeAttribute("order");
            req.setAttribute("msg", "支付成功");
        } else {
            req.setAttribute("failMsg", "支付失败！");
        }
        req.getRequestDispatcher("order_success.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
